package com.aisile.sellergoods.service.iml;

import com.aisile.commonPojo.SpectionOption;
import com.aisile.entity.PageResult;
import com.aisile.manager.SpecService;
import com.aisile.mappers.TbSpecificationMapper;
import com.aisile.mappers.TbSpecificationOptionMapper;
import com.aisile.pojo.TbSpecification;
import com.aisile.pojo.TbSpecificationExample;
import com.aisile.pojo.TbSpecificationOption;
import com.aisile.pojo.TbSpecificationOptionExample;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class SpecServiceiml implements SpecService {

    @Autowired
    TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    TbSpecificationOptionMapper tbSpecificationOptionMapper;


    public PageResult findPage(int page, int rows, TbSpecification tbSpecification) {
        PageHelper.startPage(page,rows);
        TbSpecificationExample example=new TbSpecificationExample();
        if(!StringUtils.isEmpty(tbSpecification.getSpecName())){
            example.createCriteria().andSpecNameLike("%"+tbSpecification.getSpecName()+"%");
        }
        Page<TbSpecification> p=(Page<TbSpecification>)tbSpecificationMapper.selectByExample(example);
        return new PageResult(p.getTotal(),p.getResult());
    }


    public void insertSpection(SpectionOption spectionOption) {
       tbSpecificationMapper.insert(spectionOption.getSpecEntity()) ;

       if(spectionOption.getSpecOption()!=null){
          for(TbSpecificationOption s:spectionOption.getSpecOption()){
              s.setSpecId(spectionOption.getSpecEntity().getId());
              tbSpecificationOptionMapper.insert(s);
          }
       }

    }


    public void update(SpectionOption spectionOption) {
        tbSpecificationMapper.updateByPrimaryKey(spectionOption.getSpecEntity());
        if(spectionOption.getSpecOption()!=null){
            TbSpecificationOptionExample example=new TbSpecificationOptionExample();
            example.createCriteria().andSpecIdEqualTo(spectionOption.getSpecEntity().getId());
            tbSpecificationOptionMapper.deleteByExample(example);
            for(TbSpecificationOption specificationOption:spectionOption.getSpecOption()){
               specificationOption.setSpecId(spectionOption.getSpecEntity().getId());
               tbSpecificationOptionMapper.insert(specificationOption);
            }
        }
    }


    public SpectionOption findOne(Long id) {
        SpectionOption spectionOption=new SpectionOption();
         TbSpecification specification=tbSpecificationMapper.selectByPrimaryKey(id);

         TbSpecificationOptionExample tbSpecificationOptionExample=new TbSpecificationOptionExample();
         tbSpecificationOptionExample.createCriteria().andSpecIdEqualTo(id);
         List<TbSpecificationOption> list=tbSpecificationOptionMapper.selectByExample(tbSpecificationOptionExample);
         spectionOption.setSpecEntity(specification);
         spectionOption.setSpecOption(list);
        return spectionOption;
    }


    @Override
    public List<Map> findSpecList() {
        return tbSpecificationMapper.selectOptionList();
    }

    public void deleteAll(Long[] ids) {
        if(ids!=null){
            for(Long i:ids){
                tbSpecificationMapper.deleteByPrimaryKey(i);
                TbSpecificationOptionExample example=new TbSpecificationOptionExample();
                example.createCriteria().andSpecIdEqualTo(i);
                tbSpecificationOptionMapper.deleteByExample(example);
            }
        }
    }
}
